package com.ruoyi.web.controller.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.config.OssConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
@Service
public class FileService {
    @Autowired
    private OssConfig ossConfig;
    public String uploadFile(MultipartFile file) throws IOException {
        OSS ossClient = ossConfig.ossClient();
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString() + extension;
        String objectName =  "http://" + ossConfig.getBasePath() + fileName;
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(), objectName, file.getInputStream());
        ossClient.putObject(putObjectRequest);
        return "https://" + ossConfig.getBucketName() + "." + ossConfig.getEndpoint() + "/" + objectName;
    }
}
